<template>
  <!-- 分配权限的弹层 -->
  <el-dialog
    title="设置权限"
    :visible="shouDialogPerm"
    @close="hClose"
  >
    <el-tree
      ref="tree"
      node-key="id"
      :data="permList"
      :props="{label:'name'}"
      show-checkbox
      default-expand-all
      check-strictly
    />
    <el-button
      size="mini"
      type="primary"
      @click="hSave"
    >确定</el-button>
  </el-dialog>
</template>
<script>
import { getPermission, assignPerm } from '@/api/permission'
import { arr2tree } from '@/utils/index'
import { getPermissiById } from '@/api/role'

export default {
  name: 'AssignPermission',
  props: {
    shouDialogPerm: {
      type: Boolean,
      default: false
    },
    curId: {
      type: Number,
      required: true
    }
  },
  data() {
    return {
      permList: []
    }
  },
  created() {
    // 读取所有的权限
    this.loadPermission()
  },
  methods: {
    async hSave() {
      const keys = this.$refs.tree.getCheckedKeys()
      //   console.log(keys)
      await assignPerm({ id: this.curId, permIds: keys })
      this.hClose()
    },
    async getPermiss() {
      const res = await getPermissiById(this.curId)

      // 数据回显
      this.$refs.tree.setCheckedKeys(res.data.permIds)
    },
    async loadPermission() {
      const res = await getPermission()
      console.log('获取权限点', res)
      // 数据转树状图
      const res2 = arr2tree(res.data, 0)
      this.permList = res2
    },
    hClose() {
      this.$emit('update:shouDialogPerm', false)
    }
  }
}
</script>
